package cn.gaily.eduservice.controller;


import cn.gaily.commonutils.R;
import cn.gaily.eduservice.entity.EduChapter;
import cn.gaily.eduservice.entity.vo.ChapterVo;
import cn.gaily.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author xuchengwei
 * @since 2021-07-05
 */
@Api(description = "章节接口")
@RestController
@RequestMapping("/eduservice/chapter")
@CrossOrigin
public class EduChapterController {

    @Autowired
    EduChapterService eduChapterService;

    @ApiOperation(value = "获得课程大纲列表")
    @GetMapping("/getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = eduChapterService.getChapterVideoByCourseId(courseId);
        return R.ok().data("allChapterVideo",list);
    }

    @ApiOperation(value = "获得章节")
    @GetMapping("/getChapterInfo/{chapterId}")
    public R getChapterInfo(@PathVariable String chapterId){
        EduChapter chapterInfo = eduChapterService.getById(chapterId);
        return R.ok().data("chapter",chapterInfo);
    }

    @ApiOperation(value = "添加章节")
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        boolean save = eduChapterService.save(eduChapter);
        if(save)
            return R.ok();
        else
            return R.error();
    }

    @ApiOperation(value = "更新章节")
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        boolean update = eduChapterService.updateById(eduChapter);
        if(update)
            return R.ok();
        else
            return R.error();
    }

    @ApiOperation(value = "删除章节")
    @DeleteMapping("/deleteChapter/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        int delete = eduChapterService.deleteChapterById(chapterId);
        return R.ok().data("删除章节数",delete);
    }
}

